package com.product.service.impl;

import com.aliyun.oss.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gulimall.common.utils.PageUtils;
import com.gulimall.common.utils.Query;
import com.product.dao.SkuInfoDao;
import com.product.entity.SkuImagesEntity;
import com.product.entity.SkuInfoEntity;
import com.product.entity.SpuInfoDescEntity;
import com.product.service.*;
import com.product.vo.SkuItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuImagesService imagesService;

    @Autowired
    private SpuInfoDescService spuInfoDescService;

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private SkuSaleAttrValueService saleAttrValueService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        String catalogId = (String) params.get("catelogId");
        String brandId = (String) params.get("brandId");
        String key = (String) params.get("key");
        BigDecimal max = BigDecimal.valueOf(Long.parseLong(params.get("max").toString()));
        BigDecimal min = BigDecimal.valueOf(Long.parseLong(params.get("min").toString()));
        if (!StringUtils.isNullOrEmpty(catalogId) && !"0".equals(catalogId)) {
            wrapper.eq("catalog_id", catalogId);
        }
        if (!StringUtils.isNullOrEmpty(brandId) && !"0".equals(brandId)) {
            wrapper.eq("brand_id", brandId);
        }
        if (!StringUtils.isNullOrEmpty(key)) {
            wrapper.and(w -> {
                w.like("sku_name", key).or().like("sku_title", key);
            });

        }
        if (max.compareTo(new BigDecimal(0)) > 0) {
            wrapper.lt("price", max);
        }
        if (min.compareTo(new BigDecimal(0)) > 0) {
            wrapper.ge("price", min);
        }

        IPage<SkuInfoEntity> page = this.page(new Query<SkuInfoEntity>().getPage(params), wrapper);

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {

        return this.list(new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId));
    }

    @Override
    public SkuItemVo item(Long skuId) throws ExecutionException, InterruptedException {
        SkuItemVo skuItemVo = new SkuItemVo();
        // sku 基本信息 pms_sku_info 异步编排任务
        CompletableFuture<SkuInfoEntity> infoFuture = CompletableFuture.supplyAsync(() -> {
            SkuInfoEntity skuInfoEntity = getById(skuId);
            skuItemVo.setInfo(skuInfoEntity);
            return skuInfoEntity;
        }, executor);

        // 获取 spu 的销售组合
        CompletableFuture<Void> saleAttrsFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SkuItemVo.ItemSaleAttrsVo> saleAttrsVos = saleAttrValueService.getSaleAttrsBySpuId(res.getSpuId());
            skuItemVo.setSaleAttr(saleAttrsVos);
        }, executor);

        //获取 spu 的介绍组合
        CompletableFuture<Void> descFuture = infoFuture.thenAcceptAsync((res) -> {
            SpuInfoDescEntity spuInfoDescEntity = spuInfoDescService.getById(res.getSpuId());
            skuItemVo.setDesc(spuInfoDescEntity);
        }, executor);

        //spu规格参数
        CompletableFuture<Void> baseFuture = infoFuture.thenAcceptAsync((res) -> {
            List<SkuItemVo.ItemGroupAttrsVo> groupAttrs = attrGroupService.getAttrGroupWithAttrsBySpuId(res.getSpuId(), res.getCatalogId());
            skuItemVo.setGroupAttrs(groupAttrs);
        }, executor);

        // 图片信息 pms_spu_images
        CompletableFuture<Void> imageFuture = CompletableFuture.runAsync(() -> {
            List<SkuImagesEntity> imagesEntities = imagesService.getImagesBySkuId(skuId);
            skuItemVo.setImages(imagesEntities);
        }, executor);

        CompletableFuture.allOf(infoFuture, saleAttrsFuture, baseFuture, descFuture, imageFuture).get();
        //等待所有任务完成
        return skuItemVo;
    }

}
